package com.gsbelarus.gedemin.salary.fragment.gdmsg;

import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.entity.model.GdMsgModel;
import com.gsbelarus.gedemin.salary.util.ExtractStringHelper;

public class GdMsgDetailFragment extends Fragment {

    private GdMsgModel gdMsg;

    public static GdMsgDetailFragment newInstance(GdMsgModel gdMsg) {
        Bundle args = new Bundle();
        args.putSerializable("gdMsg", gdMsg);

        GdMsgDetailFragment fragment = new GdMsgDetailFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gdMsg = (GdMsgModel) getArguments().getSerializable("gdMsg");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.gdmsg_detail_fragment, container, false);

        ImageView avatarImageView = (ImageView) view.findViewById(R.id.image_view_avatar);
        TextView typeTextView = (TextView) view.findViewById(R.id.txt_type);
        TextView dateTextView = (TextView) view.findViewById(R.id.txt_date);
        TextView subjectTextView = (TextView) view.findViewById(R.id.txt_subject);
        TextView msgTextView = (TextView) view.findViewById(R.id.txt_msg);
        TextView titleReplyTextView = (TextView) view.findViewById(R.id.tvReplyTitle);
        TextView msgReplyTextView = (TextView) view.findViewById(R.id.tvReplyMsg);
        TextView titleCommentTextView = (TextView) view.findViewById(R.id.txt_title_comment);
        TextView commentTextView = (TextView) view.findViewById(R.id.txt_comment);

        if (gdMsg.getSender() == GdMsgModel.Sender.SERVER) {
            avatarImageView.setImageResource(R.mipmap.ic_arrow_right_white_36dp);
        } else {
            avatarImageView.setImageResource(R.mipmap.ic_arrow_left_white_36dp);
        }

        switch (gdMsg.getRequestStatus()) {
            case SUCCESS:
                ((GradientDrawable) avatarImageView.getBackground()).setColor(getResources().getColor(R.color.debit_color));
                break;
            case UNKNOWN:
                ((GradientDrawable) avatarImageView.getBackground()).setColor(getResources().getColor(R.color.light_gray));
                break;
            case ERROR:
            case REJECTED:
                ((GradientDrawable) avatarImageView.getBackground()).setColor(getResources().getColor(R.color.credit_color));
                break;
        }

        String type = ExtractStringHelper.getGdMsgSender(gdMsg);

        typeTextView.setText(type);
        dateTextView.setText(ExtractStringHelper.getGdMsgLongFormattedTimestamp(gdMsg));

        String subject = ExtractStringHelper.getGdMsgRequestSubject(gdMsg);
        subjectTextView.setText(subject);

        String status = ExtractStringHelper.getGdMsgRequestStatus(gdMsg);
        String data = ExtractStringHelper.getGdMsgData(gdMsg);
        if (!data.equals("")) {
            data += ". ";
        }
        if ((!status.equals("")) && (!data.equals(""))) {
            status += "\n";
        }

        msgTextView.setText(status + data);

        if (!gdMsg.getComment().equals("")) {
            commentTextView.setText(gdMsg.getComment());

            titleCommentTextView.setVisibility(View.VISIBLE);
            commentTextView.setVisibility(View.VISIBLE);
        } else {
            titleCommentTextView.setVisibility(View.GONE);
            commentTextView.setVisibility(View.GONE);
        }


        GdMsgModel requestGdMsg = null;
        if (gdMsg.getSender() == GdMsgModel.Sender.SERVER) {
            switch (gdMsg.getSubject()) {
                case HOLIDAY:
                    requestGdMsg = gdMsg.getGdMsgHolidayModel().getRequestGdMsgModel();
                    break;
                case VACATION:
                    requestGdMsg = gdMsg.getGdMsgVacationModel().getRequestGdMsgModel();
                    break;
                case INCOME:
                    requestGdMsg = gdMsg.getGdMsgIncomeModel().getRequestGdMsgModel();
                    break;
                case CHANGE_USER_PROFILE:
                    requestGdMsg = gdMsg.getGdMsgEmployeeModel().getRequestGdMsgModel();
                    break;
                case CHANGE_WORK_SCHEDULE:
                    requestGdMsg = gdMsg.getGdMsgWorkScheduleModel().getRequestGdMsgModel();
                    break;
                case INFO:
                    requestGdMsg = gdMsg.getGdMsgInfoModel().getRequestGdMsgModel();
                    break;
            }
        }
        if (requestGdMsg != null) {
            titleReplyTextView.setVisibility(View.VISIBLE);
            msgReplyTextView.setVisibility(View.VISIBLE);

            titleReplyTextView.setText(getResources().getString(R.string.reply_title, ExtractStringHelper.getGdMsgLongFormattedTimestamp(requestGdMsg)));

            String requestData = ExtractStringHelper.getGdMsgData(requestGdMsg);
            if (!requestData.equals("")) {
                requestData += ". ";
            }
            msgReplyTextView.setText(requestData + requestGdMsg.getComment());
        } else {
            titleReplyTextView.setVisibility(View.GONE);
            msgReplyTextView.setVisibility(View.GONE);
        }

        return view;
    }
}
